package com.hyj.jpdy.job.RunModle;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hyj.jpdy.bean.vo.Article;
import com.hyj.jpdy.grap.html.GrapHtmlUtil;
import com.hyj.jpdy.grap.html.JokeGrap;
import com.hyj.jpdy.service.IGrapService;

public class JokeModle implements Runnable {

	private IGrapService grapService;
	Log log = LogFactory.getLog(JokeModle.class);
	
	public JokeModle(IGrapService grapService){
		this.grapService = grapService;
	}
	
	@Override
	public void run() {
		 String url = "http://www.jokeji.cn/";
		 Document doc = GrapHtmlUtil.getDocument(url,"GBK");
		 if(doc !=null){
			 // 最新笑话
			 List<Article> list = JokeGrap.grapNewJoke(doc,url);
			 for(Article i:list){
				 try{ 
				   i.setTypeId(11);
				   grapService.saveArticle(i);
			       
			    }catch(Exception e){
			       e.printStackTrace();
			    }
			 }
			 
			//  笑话分类
			/*Elements typeList = doc.getElementsByAttributeValue("class", "joketype l_left").get(0)
			.getElementsByTag("a");
			for(Element e:typeList){
				String href = e.attr("href");
				String type = e.text();
				log.info("----------------->>类型："+type+"   地址："+url+href);
		        list=JokeGrap.grapType(url,url+href);
		        for(Article i:list){
					 try{ 
					   grapService.saveArticle(i);
				    }catch(Exception e1){
				       e1.printStackTrace();
				    }
				 }
			}*/
			 List<Map<String,Object>> typeList =  grapService.queryJokeColumn ();
			 for(Map<String,Object> m:typeList){
					String href = (String) m.get("url");
					int typeId = (Integer) m.get("categoreId");
					int modleId = (Integer) m.get("modleId");
					log.info("typeId："+typeId+"   url："+href);
					if(modleId == 1){
						// http://www.jokeji.cn/list29_1.htm
						// http://www.jokeji.cn/list29_2.htm
						for(int i = 0;i<5;i++){
					    href = urlAdd(href,i);
						log.info(" 分类地址： "+href);
				        list=JokeGrap.grapType(url,href);
				        for(Article artic:list){
							 try{ 
							   artic.setTypeId(typeId);
							   grapService.saveArticle(artic);
						    }catch(Exception e1){
						       e1.printStackTrace();
						    }
						 }
					  }
					}else if(modleId == 2){
						
					}
				}

	}
}
	
	/**
	 * http://www.jokeji.cn/list29_1.htm
	 * 增加为
	 * http://www.jokeji.cn/list29_2.htm
	 * 每次增加 i
	 * @param url
	 * @return
	 */
	public  static String urlAdd(String url,int i){
		if(i==0){
			return url;
		}else{
			try {
				String strHead = url.substring(0, url.lastIndexOf("."));
				String strTail = url.substring(url.lastIndexOf("."));
				int num = Integer.parseInt(strHead.substring(
						strHead.length() - 1, strHead.length()));
				num +=i;
				strHead = strHead.substring(0, strHead.length() - 1);

				return strHead + num + strTail;
			} catch (Exception e) {
				//e.printStackTrace();
				return "";
			}
		}
	}
}
